package com.workstudy.web.controller.workstudy;

import java.util.List;

import com.workstudy.common.core.domain.entity.SysRole;
import com.workstudy.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.workstudy.common.annotation.Log;
import com.workstudy.common.enums.BusinessType;
import com.workstudy.workstudy.domain.WorkstudyPost;
import com.workstudy.workstudy.service.IWorkstudyPostService;
import com.workstudy.common.core.controller.BaseController;
import com.workstudy.common.core.domain.AjaxResult;
import com.workstudy.common.utils.poi.ExcelUtil;
import com.workstudy.common.core.page.TableDataInfo;

/**
 * 岗位Controller
 * 
 * @author ws
 * @date 2021-03-28
 */
@Controller
@RequestMapping("/workstudy/post")
public class WorkstudyPostController extends BaseController
{
    private String prefix = "workstudy/post";

    @Autowired
    private IWorkstudyPostService workstudyPostService;

    @RequiresPermissions("workstudy:post:view")
    @GetMapping()
    public String post()
    {
        return prefix + "/post";
    }

    /**
     * 查询岗位列表
     */
    @RequiresPermissions("workstudy:post:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WorkstudyPost workstudyPost)
    {
        //获取登录的用户role
        List<SysRole> roleList = ShiroUtils.getSysUser().getRoles();
        for(SysRole role:roleList){
            //如果是用人单位就查出他指导的岗位
            if("employer".equals(role.getRoleKey())){
                workstudyPost.setAnnouncerBy(ShiroUtils.getLoginName());
            }
        }
        startPage();
        List<WorkstudyPost> list = workstudyPostService.selectWorkstudyPostList(workstudyPost);
        return getDataTable(list);
    }

    /**
     * 导出岗位列表
     */
    @RequiresPermissions("workstudy:post:export")
    @Log(title = "岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WorkstudyPost workstudyPost)
    {
        List<WorkstudyPost> list = workstudyPostService.selectWorkstudyPostList(workstudyPost);
        ExcelUtil<WorkstudyPost> util = new ExcelUtil<WorkstudyPost>(WorkstudyPost.class);
        return util.exportExcel(list, "post");
    }

    /**
     * 新增岗位
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("workstudy:post:add")
    @Log(title = "岗位", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkstudyPost workstudyPost)
    {
        workstudyPost.setAnnouncerBy(ShiroUtils.getLoginName());
        return toAjax(workstudyPostService.insertWorkstudyPost(workstudyPost));
    }

    /**
     * 修改岗位
     */
    @GetMapping("/edit/{postId}")
    public String edit(@PathVariable("postId") Long postId, ModelMap mmap)
    {
        WorkstudyPost workstudyPost = workstudyPostService.selectWorkstudyPostById(postId);
        mmap.put("workstudyPost", workstudyPost);
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("workstudy:post:edit")
    @Log(title = "岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkstudyPost workstudyPost)
    {
        workstudyPost.setAnnouncerBy(ShiroUtils.getLoginName());
        return toAjax(workstudyPostService.updateWorkstudyPost(workstudyPost));
    }

    /**
     * 删除岗位
     */
    @RequiresPermissions("workstudy:post:remove")
    @Log(title = "岗位", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workstudyPostService.deleteWorkstudyPostByIds(ids));
    }

    /**
     * 修改审核
     */
    @GetMapping("/appr/{id}")
    public String appr(@PathVariable("id") Long id, ModelMap mmap)
    {
        WorkstudyPost workstudyPost = workstudyPostService.selectWorkstudyPostById(id);
        mmap.put("workstudyPost", workstudyPost);
        return prefix + "/appr";
    }

    /**
     * 审核
     */
    @RequiresPermissions("workstudy:post:appr")
    @Log(title = "岗位", businessType = BusinessType.UPDATE)
    @PostMapping("/appr")
    @ResponseBody
    public AjaxResult appr(WorkstudyPost workstudyPost)
    {
        workstudyPost.setApprovalBy(ShiroUtils.getLoginName());
        return toAjax(workstudyPostService.updateWorkstudyPostAppr(workstudyPost));
    }
}
